<?php 
// En las siguientes lineas guardamos en la variable "final" la ultima parte de la url, lo que servirá para indicar luego en el menu en que seccion estamos.
// In the following lines we save in the variable "final"" the last part of the url, which will indicate later in which section in the menu we are.

$url=$_SERVER['SCRIPT_NAME'];
$last=explode('/', $url); 

for ($i=0;$i<=sizeof($last)-1;$i++) 
{
	if ($i==sizeof($last)-1)
	{
		$final=$last[$i];
	}
}

include ('start.php');;
?>

<h2>NEW PARTICIPANT</h2><br />

<?php
/* This script adds a new Regatta entry to the database. */

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

	require("mysqlsettings.php");

	// Connect and select:
	$dbc = mysql_connect($server, $username, $password);
	mysql_set_charset('utf8', $dbc);
	mysql_select_db('db.Regatta', $dbc);
	
	// Validate and secure the form data:
	$problem = FALSE;
	if (!empty($_POST['Name']) && !empty($_POST['Skipper']) && !empty($_POST['BoatClass'])) {
		$Name = mysql_real_escape_string(trim(strip_tags($_POST['Name'])), $dbc);
		$Skipper = mysql_real_escape_string(trim(strip_tags($_POST['Skipper'])), $dbc);
		$BoatClass = mysql_real_escape_string(trim(strip_tags($_POST['BoatClass'])), $dbc);
	} else {
		print '<p style="color: red;">Please submit a name, a skipper and the boat class.</p>';
		$problem = TRUE;
	}

	if (!$problem) {

		// Define the query:
		$query = "INSERT INTO Boat (Boat_Name, Skipper, BoatClass) VALUES ('$Name', '$Skipper', '$BoatClass')";
		
		// Execute the query:
		if (@mysql_query($query, $dbc)) {
			print '<p style="color: green;">Boat participant added</p>';
		} else {
			print '<p style="color: red;">Could not add the boat because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
		}
	
	} // No problem!

	mysql_close($dbc); // Close the connection.
	
} // End of form submission IF.

// Display the form:
?>
<form action="" method="post">
	<p>Enter Name: <br /><input type="text" name="Name" size="40" maxsize="100" /></p>
	<p>Enter the skipper: <br /><input type="text" name="Skipper" /></p>
	<p>Choose the boat class: <br />
		<select name="BoatClass">
  				<option value="Optimist">Optimist</option>
  				<option value="420">420</option>
  				<option value="Laser">Laser</option>
  				<option value="Snipe">Snipe</option>
		</select></p>
	<input type="submit" name="submit" value="Register Participant" />
</form>

<?php 
include ('includes/footer.php');
?>
